/**
 * jQuery Nadeb lightBox plugin
 *
 *
 * @name    jquery.nadeb.lightbox1.0.1.js
 * @author  Mateus Martins
 * @version 1.0.1
 * @date    30.09.2012
 * @category jQuery plugin
 * @copyright (c) Mateus Martins
 * @license CC Attribution-No Derivative Works 2.5 Brazil - http://creativecommons.org/licenses/by-nd/2.5/br/deed.en_US
 * @example $('class_or_id_to_tag_link_for_image').nadebLightBox();
 */

// Offering a Custom Alias suport - More info: http://docs.jquery.com/Plugins/Authoring#Custom_Alias
(function($)
{
	$.fn.nadebLightBox = function(callback, settings)
	{
		$('.lightbox').each(function(i)
		{
			$(this).attr('data-index', i)
		});

		var config = {
				'bgColor'    : '#000',
				'bgOpacity'  : 0.5,
				'urlLoader'  : 'http://nadeb-lightbox.googlecode.com/svn/trunk/img/loading.gif',
				'close'      : 'http://nadeb-lightbox.googlecode.com/svn/trunk/img/close.png',
				'next'       : 'http://nadeb-lightbox.googlecode.com/svn/trunk/img/next.png',
				'back'       : 'http://nadeb-lightbox.googlecode.com/svn/trunk/img/back.png'
		};
		var events = {
			close : function()
			{
				$('.ltBox_bg, .ltBox_window, .ltBox_container, .ltBox_wrapper').remove();
			},
			back : function()
			{
				var index = $(_obj).data('index') - 1;
				_obj = $('.lightbox:eq('+ (index == -1 ? $('.lightbox').length-1 : index) +')');
				events.hiddenImage.call();
			},
			next : function()
			{
				var index = $(_obj).data('index') + 1;
				_obj = $('.lightbox:eq('+ ($('.lightbox').length == index ? 0 : index) +')');
				events.hiddenImage.call();
			},
			hiddenImage : function()
			{
				$('.ltBox_window .back, .ltBox_window .next').fadeTo('fast',0);
				$('.ltBox_container').fadeTo('slow',0,function()
				{
					$(_obj).nadebLightBoxShow( callback );
				});
			}
		};

		if ( settings ) $.extend(config, settings);
		if ( !callback ) callback = false;

		var _obj;
		this.click(function()
		{
			_obj = this;
			$('body').append(
				'<div class="ltBox_bg"></div>'+
				'<div class="ltBox_window">'+
					'<div class="close">[ x ]</div>'+
					'<div class="back">[ < ]</div>'+
					'<div class="next">[ > ]</div>'+
				'</div>'+
				'<div class="ltBox_wrapper"></div>'+
				'<div class="ltBox_container"></div>'
			);

			$('.ltBox_window .next').die('click',events.next);
			$('.ltBox_window .close').die('click',events.close);
			$('.ltBox_window .back').die('click',events.back);

			$('.ltBox_window .close').live('click',events.close);
			$('.ltBox_window .next').live('click',events.next);
			$('.ltBox_window .back').live('click',events.back);
			

			$('.ltBox_bg').fadeTo(0,0);
			$('.ltBox_window').fadeTo(0,0);
			$('.ltBox_wrapper').fadeTo(0,0);

			$('.ltBox_bg').css({
				'backgroundColor' : config.bgColor,
				'position' : 'fixed',
				'height'   : '100%',
				'width'    : '100%',
				'left'     : '0',
				'top'      : '0',
				'zIndex'   : '10000'
			});

			$('.ltBox_window').css({
				'position'   : 'fixed',
				'height'     : '175px',
				'width'      : '175px',
				'zIndex'     : '10004'
			});

			$('.ltBox_wrapper').css({
				'background' : '#fff url('+ config.urlLoader +') center no-repeat',
				'position'   : 'fixed',
				'height'     : '150px',
				'width'      : '150px',
				'zIndex'     : '10002'
			});


			$('.ltBox_window .close').css({
				'background' : 'url("'+ config.close +'") no-repeat 0 0',
				'height'     : '29px',
				'overflow'   : 'hidden',
				'padding'    : '0',
				'textIndent' : '-999px',
				'width'      : '29px',
				'float'      : 'right',
				'cursor'     : 'pointer'
			});

			$('.ltBox_window .back').css({
				'background' : 'url("'+ config.back +'") no-repeat 0 0',
				'height'     : '29px',
				'overflow'   : 'hidden',
				'padding'    : '0',
				'textIndent' : '-999px',
				'width'      : '29px',
				'position'   : 'absolute',
				'left'       : '0',
				'cursor'     : 'pointer',
				'zIndex'     : '1',
				'display'    : 'none'
			});

			$('.ltBox_window .next').css({
				'background' : 'url("'+ config.next +'") no-repeat 0 0',
				'height'     : '29px',
				'overflow'   : 'hidden',
				'padding'    : '0',
				'textIndent' : '-999px',
				'width'      : '29px',
				'position'   : 'absolute',
				'right'      : '0',
				'cursor'     : 'pointer',
				'zIndex'     : '1',
				'display'    : 'none'
			});

			$('.ltBox_container').css({
				'position'   : 'fixed',
				'zIndex'     : '10003'
			});

			$('.ltBox_window').css({
				'left' : ( $('.ltBox_bg').width() - $('.ltBox_window').width() ) / 2,
				'top'  : ( $('.ltBox_bg').height() - $('.ltBox_window').height() ) / 2
			});

			$('.ltBox_wrapper').css({
				'left' : ( $('.ltBox_bg').width() - $('.ltBox_wrapper').width() ) / 2,
				'top'  : ( $('.ltBox_bg').height() - $('.ltBox_wrapper').height() ) / 2
			});

			$('.ltBox_bg').fadeTo('fast',config.bgOpacity, function()
			{
				$('.ltBox_wrapper').fadeTo('slow',1);
				$('.ltBox_window').fadeTo('slow',1, function()
				{
					$(_obj).nadebLightBoxShow( callback );
				});
			});

			return false;
		});

		return this;
   };

	$.fn.nadebLightBoxShow = function(callback)
	{
		var _obj   = this;
		var imgSr  = "";
		var imgSrc = $(this).attr('href');
		var img    = new Image();

		$(img).load(function ()
		{
			var imgWidth   = this.width;
		    var imgHeight  = this.height;
			var sobraA     = 0;
			var sobraB     = 0;

			$('.ltBox_container').css({
				'left' : ( $('.ltBox_bg').width() - (imgWidth) ) / 2,
				'top'  : ( $('.ltBox_bg').height() - (imgHeight) ) / 2
			});

			if( $('.ltBox_bg').width() < imgWidth )
			{
				imgWidth = $('.ltBox_bg').width() - 60;
				$('.ltBox_container').css({
					'left'     :  ( $('.ltBox_bg').width() - (imgWidth) ) / 2,
					'width'    :  imgWidth,
					'overflow' :  'auto'
				});
			}
			if( $('.ltBox_bg').height() < imgHeight )
			{
				imgHeight = $('.ltBox_bg').height() - 70;
				$('.ltBox_container').css({
					'top'      : ( $('.ltBox_bg').height() - (imgHeight) ) / 2,
					'height'   : imgHeight,
					'overflow' : 'auto'
				});

			}

			if( $('.ltBox_bg').height() < this.height && $('.ltBox_bg').width() > this.width )
			{
				sobraA = 20;
				$('.ltBox_container').css('width', imgWidth + 22);
			}

			if( $('.ltBox_bg').height() > this.height && $('.ltBox_bg').width() < this.width )
			{
				sobraB = 20;
				$('.ltBox_container').css('height', imgHeight + 22);
			}

			$('.ltBox_wrapper').animate({
				width : imgWidth + 20 + sobraA,
				height: imgHeight + 20 + sobraB,
				left  : ( $('.ltBox_bg').width() - (imgWidth + 20) ) / 2,
				top   : ( $('.ltBox_bg').height() - (imgHeight + 20) ) / 2
			});
			$('.ltBox_window').animate({
				width : imgWidth + 45 + sobraA,
				height: imgHeight + 45 + sobraB,
				left  : ( $('.ltBox_bg').width() - (imgWidth + 45) ) / 2,
				top   : ( $('.ltBox_bg').height() - (imgHeight + 45) ) / 2
			},{
				duration: 500,
				complete: function() {
					setTimeout(
							function()
							{
								$('.ltBox_container').html( '<img src="'+ imgSrc +'" alt="-" />' );
								$('.ltBox_container').fadeTo(0,0,function() {
									$('.ltBox_container').fadeTo('slow',1, function(){
										callback.call( _obj );
									});
								});

								$('.ltBox_window .back, .ltBox_window .next').css({'marginTop' : ($('.ltBox_container').height() / 2) + 'px'}).fadeTo('slow',1);
							}
							, 1000
						)
				}
			});
	    }).attr('src', imgSrc );

	return this;
   }

})(jQuery);